import { Component, OnInit, ViewChild} from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';
import { ProblemAnalyseComponent } from "../+problem-analyse/problem-analyse.component"

declare var $: any;
@Component({
	selector: 'sa-weight-abnormal-detail',
	templateUrl: './weight-abnormal-detail.component.html',
	styleUrls: ['./weight-abnormal-detail.component.css']
})
export class WeightAbnormalDetailComponent implements OnInit {
	tableData = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = ''; //搜索keyword
	startTime_table:string = '';
	endTime_table:string = '';
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	carNumber:string = '';
	deviceId:string = '';
	timeType:string='1'; //时间类型
	jumpData:any; // 跳转页面接收到的数据
    initDateRange: any = {
        startTime: '',
        endTime: '',
    }// 时间组件需要渲染的值
    specifyId:any = {
        picker1: 'datePicker1',
        picker2: 'datePicker2',
    }
    companyData:any = {
        companyId: '',
        companyName: '',
    }
    carDeatailUseList = carTypeListObj.carDeatailUseList
    sortBy:number;
    sortType:number;
    columnSortFlag:number;//0kg次数排序
    @ViewChild("analyseComponent")
    parentAnalyseComponent: ProblemAnalyseComponent
	constructor(private jsonApiService: JsonApiService, private router: Router,private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage || 1;
		});
	}

	ngOnInit() {
	    let that = this;
	    this.modalMove();
        if(common.getCookie('jumpTo/environment-manage/weight-abnormal-detail')) {
            this.jumpData = JSON.parse(common.getCookie('jumpTo/environment-manage/weight-abnormal-detail'));
            if(this.jumpData.date){
                this.initDateRange.startTime = this.startTime_table = this.jumpData.date+' 04:00:00';
                this.initDateRange.endTime = this.startTime_table = this.jumpData.date+' 06:00:00';
            }
            this.companyData.companyName = this.selectedCompanyName = this.jumpData.companyName;
            this.companyData.companyId = this.selectedCompanyId = this.jumpData.companyId;
            setTimeout(()=>{
                that.load();
            },100)
            common.clearCookie('jumpTo/environment-manage/weight-abnormal-detail');
        }else{
            let date = new Date();
            let today = common.getTimeDay(date);
            this.initDateRange.startTime = this.startTime_table = today+' 04:00:00';
            this.initDateRange.endTime = this.endTime_table = today+' 06:00:00';
            setTimeout(()=>{
                that.load();
            },100)
        }
	}
	// 接收子组件传递过来的数据 
	getSelectTableTime(time){
		this.startTime_table = time.startTime;
		this.endTime_table = time.endTime;
	} 
    //公司选中回调
    companySelected(data){
        this.selectedCompanyId = data.companyId;
        this.selectedCompanyName = data.companyName;
        this.deviceId = data.deviceId;
        this.carNumber = data.carNumber;
    }
	//表格搜索
	table_search() {
	    this.columnSortFlag = null;
        this.sortBy = null;
        this.sortType = null;
		this.curPage = 1
		this.searchKey = common.stringTrim(this.searchKey);
		this.load();
	}
	//刷新
	refresh(){
    	this.load();
    }
	// 列  排序
    columnSort(val){
        this.columnSortFlag = val;
        switch (val){
            case 1:
                this.sortBy = 0;
                this.sortType = 0;
                break;
            case 2:
                this.sortBy = 0;
                this.sortType = 1;
                break;
            case 3:
                this.sortBy = 1;
                this.sortType = 0;
                break;
            case 4:
                this.sortBy = 1;
                this.sortType = 1;
                break;
            default:
                this.sortBy = null;
                this.sortType = null;
                break;
        }
        this.load();
    }
	//初始化加载list数据
	load() {
	    if(!this.startTime_table||!this.endTime_table){
            return this.jsonApiService.message('起始或结束时间不能为空');
        }
        let timeCheck=common.correctTimeParams(this.startTime_table,this.endTime_table,7);
        if(!timeCheck.status){
            this.jsonApiService.message(timeCheck.message);
            return;
        }
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId: this.selectedCompanyId,
				deviceId: this.deviceId,
				startTime:this.startTime_table,
				endTime:this.endTime_table,
				sortBy:this.sortBy,//排序  0:有0kg次数   1:有300kg次数 
                sortType:this.sortType,//排序类型 0:升序   1:降序
//				searchKey:this.searchKey,
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("abnormalWeight/queryWeightDetails.html", reqdata).subscribe(data => {
			this.tableData = data.exhibitDatas||[];
			for(let item of data.exhibitDatas) {
                item.useTypeL2 = this.getUseTypeNameL2(item.useTypeL2);
            }
			this.totalCount = data.recordCount;
		})
	}
	getUseTypeNameL2(key) {
        if(key) {
            return this.carDeatailUseList[key];
        } else {
            return '';
        }
    }
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//导出报表
	exportData() {
		if(!this.startTime_table||!this.endTime_table){
			return this.jsonApiService.message('起始或结束时间不能为空');
		}
		let timeCheck=common.correctTimeParams(this.startTime_table,this.endTime_table,7);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let parameters = {
				companyId: this.selectedCompanyId,
                deviceId: this.deviceId,
                startTime:this.startTime_table,
                endTime:this.endTime_table,
                sortBy:this.sortBy,//排序  0:有0kg次数   1:有300kg次数 
                sortType:this.sortType,//排序类型 0:升序   1:降序
			};
		let paramsName = 'param';
		let _url = config.BaseURL + 'abnormalWeight/exportWeightDetails.html';
		common.exprotDataFile(parameters,paramsName,_url);
	}
    // 重量异常明细
    rowDetail(row){
        row.initTime = true;
        row.initStartTime = row.date+' 00:00:00';
        row.initEndTime = row.date+' 23:59:59';
        this.parentAnalyseComponent.initCompanyAndCar(row);
        // 弹框展示
        $('#analyseModal').show();
        $(window).trigger("resize")
    }
    //关闭分析处理 弹框
    cancelAnalyseModal() {
        $('#analyseModal').hide();
    }

    //分析处理弹框拖动事件
    modalMove() {
        var that = this;
        $('body').undelegate('#analyseModalheadDrag', 'mousedown', function () { });
        $('body').delegate('#analyseModalheadDrag', 'mousedown', function (e) {
            $(this).css("cursor", "move");
            var offset = $('#analyseModal').position();
            var x = e.pageX - offset.left;
            var y = e.pageY - offset.top;
            $(document).bind("mousemove", function (ev) {
                $("#analyseModal").stop();
                var _x = ev.pageX - x;
                var _y = ev.pageY - y;
                if (_y < 0) { //避开头部横条
                    return;
                }
                $("#analyseModal").animate({
                    left: _x + "px",
                    top: _y + "px"
                }, 2);
            });
        });
        $(document).mouseup(function (e) {
            e.cancelBubble = true;
            $(this).unbind("mousemove");
        });
        $('body').undelegate('#dragArrow', 'mousedown', function () { });
        $('body').delegate('#dragArrow', 'mousedown', function (e) {
            let ele = $('#analyseModal');
            let x = e.pageX - ele.width();
            $('body').bind("mousemove", function (ev) {
                $("#analyseModal").stop();
                let _x = ev.pageX - x;
                if (_x < 1200) { //避开头部横条
                    return;
                }
                $("#analyseModal").find('.modal-dialog').animate({
                    width: _x + "px",
                }, 2);
                //              setTimeout(()=>{
                let W = $('#popContent').width() - 2;

                $('#echartBox').width(W * 0.55 * 0.65 - 1);
                $('#allmapBox').width(W * 0.55 * 0.35);
                $('#chart').width(W * 0.55 * 0.65 - 1);
                $(window).trigger('resize');
                //              },120)
            });
        });

        $('body').mouseup(function (e) {
            e.cancelBubble = true;
            $(this).unbind("mousemove");
            //          $(window).trigger('resize');
        });
    }
}